package cn.zqy.demo3.day01.Jdbc;

import java.sql.*;

public class Demo07JdbcResultSetNext {
    public static void main(String[] args) {
        long s = System.currentTimeMillis();
        Connection conn = null;
        Statement sta = null;
        ResultSet re = null;
        try {
            //1. 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/heima", "root", "123456");
            sta = conn.createStatement();


            String sql = "select id,name from account";
            //5.执行sql
            re = sta.executeQuery(sql);
            //6.处理结果
            //6.1 让游标向下移动一行
            while(re.next()) {
                //6.2 获取数据   参数---【数字: 列的索引。字符串：列名】
                // 注意：next()一次一动一行，
                int id = re.getInt(1);
                String name = re.getString("name");
                System.out.println(id + " ----------"+ name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //7.释放资源

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(sta != null) {
                try {
                    sta.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        long e = System.currentTimeMillis();
        System.out.println("总共耗时： " + (e - s) + "ms");
    }
}
